package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        // 创建时间
        appointment.setCreateTime(new Date());
        // 初始状态为 预约中
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        Integer status = appointmentMapper.selectByPrimaryKey(appointment.getId()).getStatus();
        // 正在预约中才可以修改，
        if (!Appointment.STATUS_APPOINTMENT.equals(status)) {
            throw new BusinessException("抱歉，只有预约中状态的预约单才可以修改~");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }


    @Override
    public void arrival(Long id) {
        // 只有预约中才可以点击到店：
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("抱歉，只有预约中状态的预约单才可以到店~");
        }
        // 点击到店确认：确定实际到店时间，把状态改为1已到店
        appointmentMapper.arrival(id,new Date(),Appointment.STATUS_ARRIVAL);

    }

    @Override
    public void remove(Long id) {
        // 只有预约中才可以点击到店：
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("抱歉，只有预约中状态的预约单才可以到店~");
        }
        // 点击删除：把预约单的状态改为 -1
        appointmentMapper.remove(id,Appointment.STATUS_DELETE);
    }

    @Override
    public void cancel(Long id) {
        // 只有预约中状态才可以取消预约单：
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("抱歉，只有预约中状态才可以取消预约单~");
        }
        // 点击取消预约单：把状态改为2
        appointmentMapper.cancel(id,Appointment.STATUS_CANCEL);
    }

    // 超时取消
    @Scheduled(fixedRate = 1000 * 60 * 60 * 24)    // SpringBoot定时器，每天执行一次
    public void ovarTimeCancel() {
        // 查询所有的预约中的单：
        List<Appointment> appointments = appointmentMapper.selectByStatusAppointment(Appointment.STATUS_APPOINTMENT);
        // 如果时间超过预约时间，则超时：
        for (Appointment appointment : appointments) {
            Date when = new Date();
            if (appointment.getAppointmentTime().before(when)) {
                //设置状态为超时取消
                appointmentMapper.cancel(appointment.getId(),Appointment.STATUS_OVERTIME);
            }
        }
    }
}
